<template>
    <h2 style="text-align: center;">房型列表</h2>
    <a href="/AddRoomType" class="btn btn-success">添加房型信息</a><br>
    房型名称:<input type="text" placeholder="请输入房型名称" v-model="name" style="margin-right: 10px;" />
    <input type="button" value="查询" class="btn btn-success" @click="ShowRoomType" />
    <table border="1" class="table table-bordered">
        <thead style="background-color: aquamarine;">
            <tr style="text-align: center;">
                <th>房型编号</th>
                <th>房型图片</th>
                <th>房型名称</th>
                <th>房型售价</th>
                <th>房型面积</th>
                <th>可住人数</th>
                <th>房型介绍</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody style="text-align: center;">
            <tr v-for="item in Info">
                <td>{{ item.RoomType_Id }}</td>
                <td> <img :src="item.RoomType_Img" style="width: 100px;height: 100px;" /></td>
                <td>{{ item.RoomType_Name }}</td>
                <td>￥{{ item.RoomType_Price.toFixed(2) }}</td>
                <td>{{ item.RoomType_Size}}平方</td>
                <td>{{ item.RoomType_Num }}人</td>
                <td>{{ item.RoomType_Info }}</td>
                <td>
                    <a @click="DelRoomType(item.RoomType_Id)" class="btn btn-danger" >删除</a>
                </td>
            </tr>
        </tbody>
    </table>
    <span>
        数据共{{ PageInfo.zongtiao }}条，每页{{ PageInfo.size }}条，共{{ PageInfo.index }}/{{ PageInfo.zongpage }}页

        <a @click="FanYe('F')" class="btn btn-success" style="margin-right: 10px;">首页</a>
        <a @click="FanYe('U')" class="btn btn-success" style="margin-right: 10px;">上一页</a>
        <a @click="FanYe('N')" class="btn btn-success" style="margin-right: 10px;">下一页</a>
        <a @click="FanYe('E')" class="btn btn-success" style="margin-right: 10px;">尾页</a>
    </span>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';


    //显示响应式数据
    const Info=ref([{
        RoomType_Id: 0,
        RoomType_Name: "",
        RoomType_Img: "",
        RoomType_Price: 0,
        RoomType_Size: "",
        RoomType_Num: "",
        RoomType_Info: "",
    }])
    //分页响应式数据
    const PageInfo=ref({
        index:1,
        size:2,
        zongtiao:0,
        zongpage:0,
    })
    //分页
    const FanYe=(chooes:string)=>{
        switch(chooes)
        {
            case"F":
                PageInfo.value.index=1;
            break;
            case"U":
                if(PageInfo.value.index>1)
                {
                    PageInfo.value.index--;
                }
                else
                {
                    alert('当前已经是第一页！');
                    return;
                }
            break;
            case"N":
                if(PageInfo.value.index<PageInfo.value.zongpage)
                {
                    PageInfo.value.index++;
                }
                else
                {
                    alert('当前已经是最后一页！');
                    return;
                }
            break;
            case"E":
                PageInfo.value.index=PageInfo.value.zongpage;
            break;
        }
        ShowRoomType();//显示
    }
    onMounted(()=>{
        ShowRoomType();//显示
    })
    //显示
    const name=ref("");
    const ShowRoomType=()=>{
        axios.get("https://localhost:44386/api/RoomType/ShowRoomType",
            {
                params:{
                    name:name.value,
                    index:PageInfo.value.index,
                    size:PageInfo.value.size,
                }
            }
        )
        .then(res=>{
            Info.value=res.data.Plist,
            PageInfo.value.zongpage=res.data.ZongPage,
            PageInfo.value.zongtiao=res.data.ZongTiao
        })
    }
    //逻辑删除
    const DelRoomType=(RoomType_Id:number)=>{
        if(confirm("确定删除?")){
            axios.delete("https://localhost:44386/api/RoomType/DelRoomType?id="+RoomType_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('删除成功');
                    ShowRoomType();//显示
                }
                else
                {
                    alert('删除失败');
                }
            })
        }
    }
</script>